extends "res://BaseEnemy.gd"

# 雪山雪人特有属性
@export var snowball_cooldown = 4.0  # 雪球冷却时间
@export var snowball_damage = 12  # 雪球伤害
@export var snowball_slow_factor = 0.6  # 雪球减速效果
@export var snowball_slow_duration = 2.0  # 雪球减速持续时间
@export var roll_cooldown = 8.0  # 滚动攻击冷却时间
@export var roll_damage = 20  # 滚动攻击伤害
@export var roll_speed_multiplier = 2.0  # 滚动速度倍数

var snowball_timer = 0.0
var roll_timer = 0.0
var can_throw_snowball = true
var can_roll = true
var is_rolling = false
var roll_target_position = Vector2.ZERO
var roll_direction = Vector2.ZERO

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置雪山雪人特有属性
	max_health = 60
	current_health = max_health
	movement_speed = 70.0
	damage = 14
	experience_value = 10
	
	# 更新生命值条
	if health_bar:
		health_bar.max_value = max_health
		health_bar.value = current_health
	
	# 设置外观
	if sprite:
		sprite.modulate = Color(1.0, 1.0, 1.0)  # 雪白色调

func _physics_process(delta):
	if is_rolling:
		# 滚动状态下的特殊移动
		velocity = roll_direction * movement_speed * roll_speed_multiplier
		move_and_slide()
		
		# 检查是否撞到玩家
		if target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance < 30:
				# 对玩家造成伤害
				target_player.take_damage(roll_damage)
				
				# 击退效果
				var knockback_direction = roll_direction
				if target_player.has_method("apply_knockback"):
					target_player.apply_knockback(knockback_direction * 200)
				
				# 结束滚动
				is_rolling = false
				print("雪人滚动攻击击中玩家!")
		
		# 检查是否滚动了足够远
		if global_position.distance_to(roll_target_position) < 20 or global_position.distance_to(roll_target_position) > 500:
			is_rolling = false
	else:
		# 非滚动状态下执行普通的物理处理
		super._physics_process(delta)
		
		# 处理雪球冷却
		if not can_throw_snowball:
			snowball_timer += delta
			if snowball_timer >= snowball_cooldown:
				can_throw_snowball = true
				snowball_timer = 0.0
		
		# 处理滚动冷却
		if not can_roll:
			roll_timer += delta
			if roll_timer >= roll_cooldown:
				can_roll = true
				roll_timer = 0.0
		
		# 如果可以投掷雪球且玩家存在，尝试投掷雪球
		if can_throw_snowball and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance <= 200 and distance > 50:  # 在适当距离时投掷雪球
				throw_snowball()
		
		# 如果可以滚动且玩家存在，尝试滚动攻击
		if can_roll and target_player and is_instance_valid(target_player):
			var distance = global_position.distance_to(target_player.global_position)
			if distance > 100 and distance < 300:  # 在适当距离时使用滚动攻击
				roll_attack()

func throw_snowball():
	if animation_player and animation_player.has_animation("throw"):
		animation_player.play("throw")
		
		# 创建雪球投射物
		var snowball = preload("res://SnowballProjectile.tscn").instantiate()
		get_tree().get_root().add_child(snowball)
		snowball.global_position = global_position
		
		# 设置雪球属性
		var direction = (target_player.global_position - global_position).normalized()
		snowball.direction = direction
		snowball.speed = 200.0
		snowball.damage = snowball_damage
		snowball.slow_factor = snowball_slow_factor
		snowball.slow_duration = snowball_slow_duration
		snowball.source = self
		
		# 重置雪球冷却
		can_throw_snowball = false
		snowball_timer = 0.0
		
		print("雪人投掷雪球!")

func roll_attack():
	if animation_player and animation_player.has_animation("roll"):
		animation_player.play("roll")
		
		# 设置滚动状态
		is_rolling = true
		can_roll = false
		roll_timer = 0.0
		
		# 计算目标位置和方向（玩家位置）
		if target_player and is_instance_valid(target_player):
			roll_target_position = target_player.global_position
			roll_direction = (roll_target_position - global_position).normalized()
		
		print("雪人开始滚动攻击!")

# 重写攻击方法
func attack_player():
	# 普通攻击
	if animation_player and animation_player.has_animation("attack"):
		animation_player.play("attack")
	
	# 对玩家造成伤害
	if target_player and target_player.has_method("take_damage"):
		target_player.take_damage(damage)
		
		# 尝试造成减速效果
		if target_player.has_method("apply_slow_effect"):
			target_player.apply_slow_effect(0.8, 1.0)  # 轻微减速
		
		print("雪人的攻击造成", damage, "伤害!")
